package com.platform.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.base.PageUtil;
import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.dto.SysLogApiDTO;
import com.platform.dto.SysLogErrorDTO;
import com.platform.dto.SysLogLoginDTO;
import com.platform.entity.ExcelColumns;
import com.platform.ext.offices.poi.excel.ExcelUtil;
import com.platform.ext.offices.poi.utils.DownLoadUtil;
import com.platform.service.SysLogApiService;
import com.platform.service.SysLogErrorService;
import com.platform.service.SysLogLoginService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p> Title:Rz Mapper</p>
 * <p> Description:  系统日志表</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Controller
@RequestMapping("/logs") 
@Api(tags="OMS-操作日志模块")
public class LogsController extends BaseController{
	
	@Autowired
	private SysLogErrorService errorLogService;
	
	@Autowired
	private SysLogApiService logApiService;
	
	@Autowired
	private SysLogLoginService logLoginService;
	
	@RequestMapping(value="findLoginLogs",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="分页查询登录日志数据")
   	@ApiImplicitParams({
           @ApiImplicitParam(name = "logs", value = "数据实体", required = true, dataType = "SysLogLoginDTO"),
           @ApiImplicitParam(name = "start", value = "起始页", required = true, dataType = "int"),
           @ApiImplicitParam(name="limit",value="每页显示大小",required = true,dataType="int")
   	})
    public Object findErrorLogs(SysLogLoginDTO logs, Integer start,Integer limit,
    		HttpServletRequest request, HttpServletResponse response,HttpSession session) {
        PageUtil pageUt = new PageUtil();
        pageUt.setPageIndex(start);
        pageUt.setPageSize(limit);
        pageUt.setParams(BeanUtil.beanToMap(logs));
        List<SysLogLoginDTO> logLists = logLoginService.selectByMapPaging(pageUt);
        pageUt.setTotal(pageUt.getTotal());
        return R.success(logLists, pageUt.getTotal());
    }
	
    @RequestMapping(value="findErrorLogs",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="分页查询系统运行错误日志数据")
   	@ApiImplicitParams({
           @ApiImplicitParam(name = "logs", value = "数据实体", required = true, dataType = "SysLogErrorDTO"),
           @ApiImplicitParam(name = "start", value = "起始页", required = true, dataType = "int"),
           @ApiImplicitParam(name="limit",value="每页显示大小",required = true,dataType="int")
   	})
    public Object findErrorLogs(SysLogErrorDTO logs, Integer start,Integer limit,
    		HttpServletRequest request, HttpServletResponse response,HttpSession session) {
        PageUtil pageUt = new PageUtil();
        pageUt.setPageIndex(start);
        pageUt.setPageSize(limit);
        pageUt.setParams(BeanUtil.beanToMap(logs));
        List<SysLogErrorDTO> logLists = errorLogService.selectByMapPaging(pageUt);
        pageUt.setTotal(pageUt.getTotal());
        return R.success(logLists, pageUt.getTotal());
    }
    
    @RequestMapping(value="findApiLogs",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="分页查询系统操作日志数据")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "logs", value = "数据实体", required = true, dataType = "SysLogApiDTO"),
    	@ApiImplicitParam(name = "start", value = "起始页", required = true, dataType = "int"),
    	@ApiImplicitParam(name="limit",value="每页显示大小",required = true,dataType="int")
    })
    public Object findApiLogs(SysLogApiDTO logs, Integer start,Integer limit,
    		HttpServletRequest request, HttpServletResponse response,HttpSession session) {
    	PageUtil pageUt = new PageUtil();
    	pageUt.setPageIndex(start);
    	pageUt.setPageSize(limit);
    	pageUt.setParams(BeanUtil.beanToMap(logs));
    	List<SysLogApiDTO> logLists = logApiService.selectByMapPaging(pageUt);
    	pageUt.setTotal(pageUt.getTotal());
    	return R.success(logLists, pageUt.getTotal());
    }
    
    @ApiOperation(value="导出系统登录日志数据")
	@RequestMapping(value = "/exportToExcelByLoginLogs")
	public void exportToExcelByLoginLogs(@RequestParam Map<String,Object> params)  throws IOException{
		//参数解析
    	List<ExcelColumns> columns = JSONUtil.parseArray(params.get("columns").toString()).toList(ExcelColumns.class);
		boolean isPDF = params.get("toPdf") != null?Boolean.parseBoolean(params.get("toPdf").toString()):false;	//是否导出当前页
		String protalParamsStr = params.get("protalParams").toString();
		@SuppressWarnings("unchecked")
		Map<String,Object> protalParams = JSONUtil.toBean(protalParamsStr, Map.class);
	    boolean colorless 	= Boolean.parseBoolean(params.get("colorless").toString());
	    boolean usemonetary = Boolean.parseBoolean(params.get("usemonetary").toString());
	    boolean sumless 	= Boolean.parseBoolean(params.get("sumless").toString());
	    boolean splitCell 	= Boolean.parseBoolean(params.get("splitCell").toString());
	    boolean watermark 	= Boolean.parseBoolean(params.get("watermark").toString());
	    String openPassword = params.get("openPassword").toString();
	    String userName		= params.get("userName").toString();
	    String deptname		= params.get("deptname").toString();
	    String systemName	= params.get("systemName").toString();
	    String modularName = params.get("modularText").toString();
		//生成
		PageUtil pg = new PageUtil();
		pg.setPageIndex(Integer.valueOf(params.get("currentPage").toString()));
		pg.setPageSize(Integer.valueOf(params.get("pageSize").toString()));
		pg.setParams(protalParams);
	    List<SysLogLoginDTO> exportData = logLoginService.selectByMapPaging(pg);
		OutputStream osExcel = ExcelUtil.generateExcelOrPDF(modularName,exportData,protalParams,columns,colorless,usemonetary,sumless,splitCell,watermark,openPassword,userName,deptname,systemName);
		if(isPDF) {
			OutputStream osPDF = DownLoadUtil.excelStreamToPDFStream(new ByteArrayInputStream(((ByteArrayOutputStream) osExcel).toByteArray()));
			String fn = modularName + "列表.pdf";
			download(osPDF, fn, response);
		}else {
			String fn = modularName + "列表.xls";
			download(osExcel, fn, response);
		}
	}
    
    @ApiOperation(value="导出系统运行错误日志数据")
	@RequestMapping(value = "/exportToExcelByErrorLogs")
	public void exportToExcel(@RequestParam Map<String,Object> params)  throws IOException{
		//参数解析
    	List<ExcelColumns> columns = JSONUtil.parseArray(params.get("columns").toString()).toList(ExcelColumns.class);
		boolean isPDF = params.get("toPdf") != null?Boolean.parseBoolean(params.get("toPdf").toString()):false;	//是否导出当前页
		String protalParamsStr = params.get("protalParams").toString();
		@SuppressWarnings("unchecked")
		Map<String,Object> protalParams = JSONUtil.toBean(protalParamsStr, Map.class);
	    boolean colorless 	= Boolean.parseBoolean(params.get("colorless").toString());
	    boolean usemonetary = Boolean.parseBoolean(params.get("usemonetary").toString());
	    boolean sumless 	= Boolean.parseBoolean(params.get("sumless").toString());
	    boolean splitCell 	= Boolean.parseBoolean(params.get("splitCell").toString());
	    boolean watermark 	= Boolean.parseBoolean(params.get("watermark").toString());
	    String openPassword = params.get("openPassword").toString();
	    String userName		= params.get("userName").toString();
	    String deptname		= params.get("deptname").toString();
	    String systemName	= params.get("systemName").toString();
	    String modularName = params.get("modularText").toString();
		//生成
		PageUtil pg = new PageUtil();
		pg.setPageIndex(Integer.valueOf(params.get("currentPage").toString()));
		pg.setPageSize(Integer.valueOf(params.get("pageSize").toString()));
		pg.setParams(protalParams);
	    List<SysLogErrorDTO> exportData = errorLogService.selectByMapPaging(pg);
		OutputStream osExcel = ExcelUtil.generateExcelOrPDF(modularName,exportData,protalParams,columns,colorless,usemonetary,sumless,splitCell,watermark,openPassword,userName,deptname,systemName);
		if(isPDF) {
			OutputStream osPDF = DownLoadUtil.excelStreamToPDFStream(new ByteArrayInputStream(((ByteArrayOutputStream) osExcel).toByteArray()));
			String fn = modularName + "列表.pdf";
			download(osPDF, fn, response);
		}else {
			String fn = modularName + "列表.xls";
			download(osExcel, fn, response);
		}
	}
    
    @ApiOperation(value="导出系统操作日志数据")
    @RequestMapping(value = "/exportToExcelByLogAPI")
    public void exportToExcelByLogAPI(@RequestParam Map<String,Object> params)  throws IOException{
    	//参数解析
    	List<ExcelColumns> columns = JSONUtil.parseArray(params.get("columns").toString()).toList(ExcelColumns.class);
    	boolean isPDF = params.get("toPdf") != null?Boolean.parseBoolean(params.get("toPdf").toString()):false;	//是否导出当前页
    	String protalParamsStr = params.get("protalParams").toString();
    	@SuppressWarnings("unchecked")
    	Map<String,Object> protalParams = JSONUtil.toBean(protalParamsStr, Map.class);
    	boolean colorless 	= Boolean.parseBoolean(params.get("colorless").toString());
    	boolean usemonetary = Boolean.parseBoolean(params.get("usemonetary").toString());
    	boolean sumless 	= Boolean.parseBoolean(params.get("sumless").toString());
    	boolean splitCell 	= Boolean.parseBoolean(params.get("splitCell").toString());
    	boolean watermark 	= Boolean.parseBoolean(params.get("watermark").toString());
    	String openPassword = params.get("openPassword").toString();
    	String userName		= params.get("userName").toString();
    	String deptname		= params.get("deptname").toString();
    	String systemName	= params.get("systemName").toString();
    	String modularName = params.get("modularText").toString();
    	//生成
    	PageUtil pg = new PageUtil();
    	pg.setPageIndex(Integer.valueOf(params.get("currentPage").toString()));
    	pg.setPageSize(Integer.valueOf(params.get("pageSize").toString()));
    	pg.setParams(protalParams);
    	List<SysLogApiDTO> exportData = logApiService.selectByMapPaging(pg);
    	OutputStream osExcel = ExcelUtil.generateExcelOrPDF(modularName,exportData,protalParams,columns,colorless,usemonetary,sumless,splitCell,watermark,openPassword,userName,deptname,systemName);
    	if(isPDF) {
    		OutputStream osPDF = DownLoadUtil.excelStreamToPDFStream(new ByteArrayInputStream(((ByteArrayOutputStream) osExcel).toByteArray()));
    		String fn = modularName + "列表.pdf";
    		download(osPDF, fn, response);
    	}else {
    		String fn = modularName + "列表.xls";
    		download(osExcel, fn, response);
    	}
    }
	
}
